package controllers;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.commons.io.FileUtils;
import play.Play;
import play.mvc.Controller;
import play.mvc.With;
import service.PictureService;
import util.ResourceOperate;
import models.Picture;

@With(Secure.class)
public class PictureAction extends Controller {

	public static String[] types = { "bmp", "jpg", "jpeg", "png", "gif", "tif",
			"zip" };

	/**
	 * 图片全查
	 */

	@Check("ResourceManage")
	public static void list() {
		List<Picture> picList = PictureService.findAll();
		render(picList);
	}

	@Check("ResourceManage")
	public static void blank() {
		render();
	}

	/**
	 * 添加图片
	 * @param picName
	 * @param fileData
	 * @param picDis
	 */
	@Check("ResourceManage")
	public static void createPic(File file, String picDis) {
		try{
			boolean b = (Boolean) ResourceOperate.upload(file,types).get(0);
			if(b){
				String uploadPath = ResourceOperate.upload(file, types).get(1).toString();
				Picture pic = new Picture();
				pic.picName = ResourceOperate.splitStr(file.getName()).get(0).toString(); // 拿到文件名
				pic.picPath = uploadPath;
				pic.picDis = picDis;
				PictureService.create(pic);
				flash.success("上传成功 ！");
				list();
			}else{
				flash.success("上传失败 ！");
				list();
			}
		} catch (Exception e) {
			blank();
			flash.success("上传失败 ！");
			e.printStackTrace();
			list();
		}
	}

	/**
	 * 修改信息
	 * @param id
	 */
	@Check("ResourceManage")
	public static void show(long id) {
		Picture pic = PictureService.findById(id);
		render(pic);
	}

	/**
	 * 修改图片信息
	 * @param picName
	 * @param picPath
	 * @param picDis
	 * @param file
	 * @param id
	 */
	@Check("ResourceManage")
	public static void savePic(String picName, String picPath, String picDis,
			File file, long id) {
		try {

			Picture pic = PictureService.findById(id);
			String oldName = pic.picName;
			pic.picName = picName;
			pic.picDis = picDis;
			if (file != null) {
				boolean bb = (Boolean) ResourceOperate.upload(file,types).get(0);
				if(bb){
					boolean b = ResourceOperate.deleteFile(Play.applicationPath + picPath);
					if (b) {
						String uploadPath = ResourceOperate.upload(file, types).get(1).toString();
						String ends =  ResourceOperate.splitStr(uploadPath).get(1).toString();
						pic.picPath = uploadPath.replace(file.getName().toString(), picName+"."+ends); // 原名
						ResourceOperate.reName(uploadPath, picName); 		// 修改文件夹中的文件名称
					}
					PictureService.save(pic); 	// 将修改后的数据保存到数据库中
					flash.success("修改成功 ！");
					list();
				}else{
					flash.success("修改失败 ！");
					list();
				}	
			} else {
				String path = picPath.replace(oldName, picName); // 将目录下的文件名称修改
				pic.picPath = path;
				ResourceOperate.reName(picPath, picName);
				PictureService.save(pic); 	// 将修改后的数据保存到数据库中
				flash.success("修改成功 ！");
				list();
			}
				
		} catch (Exception e) {
			flash.success("修改失败 ！");
			list();
			e.printStackTrace();
		}
	}

	/**
	 * 删除图片
	 * @param id
	 */
	@Check("ResourceManage")
	public static void deletePic(long id) {
		String picPath = Play.applicationPath
				+ PictureService.findById(id).picPath;
		boolean b = ResourceOperate.deleteFile(picPath);
		if (b) {
			PictureService.delete(id);
			flash.success("删除成功 ！");
		} else {
			flash.success("删除失败 ！");
		}
		list();
	}

}
